import { getUserPayload } from '@/api/utils/submit-token';

// 匿名允许路由名
const isAnonymousRoute = (route) => {
  return route.meta && route.meta.some((item) => item.anonymous);
};

export default ({ store, route, redirect, $siteConfig }) => {
  if (!$siteConfig.site || !$siteConfig.newCompanyId) {
    return;
  }

  if (isAnonymousRoute(route)) return;

  const payload = getUserPayload();

  if (!payload || payload.expired) {
    route.name !== 'login' && redirect({ name: 'login' });
  } else {
    // 获取主账户信息
    // eslint-disable-next-line no-lonely-if
    // TODO: 接口不兼容
    // if (!store.getters['user/accountId']) {
    //   await store.dispatch('user/getAccount');
    // }
    // TODO: do something else
  }
};
